package org.helgoboss.one_filer_ref_impl

import java.io.File
import org.helgoboss.project_data.ProjectData
import org.helgoboss.code_meta_data_extractor.CodeMetaDataExtractor
import org.helgoboss.scala_additions.Implicits._
import org.helgoboss.one_filer_template.OneFilerTemplate
import org.helgoboss.one_filer.OneFiler

class OneFilerRefImpl(lookupTemplate: String => Option[OneFilerTemplate]) extends OneFiler {
    def extractProjectData(sourceFile: File) = {
        val sourceCode = sourceFile.content
        val projectMapExtractor = new CodeMetaDataExtractor(trigger = "@project")
        val projectMap = projectMapExtractor.extractMetaData(sourceCode)
        val templateId = projectMap.getOrElse("templateId", "org.helgoboss.basic").toString
        lookupTemplate(templateId) match {
            case Some(t) =>
                t.extractProjectData(projectMap = projectMap, sourceCode = sourceCode, sourceFile = sourceFile)
            case None =>
                error("template [" + templateId + "] not available")
        }
    }
    
    def extractProjectDatas(sourceDir: File) = {
        sourceDir.listFiles.filter(_.getName.endsWith(".scala")).map(extractProjectData)
    }
}